File: /var/www/html/orbidirectory.com/app/Http/Controllers/CargoSpecificationController.php
<?php
namespace App\Http\Controllers;
use App\Models\CargoBrand;
use App\Models\CargoModel;
use App\Models\CargoType;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use Yajra\DataTables\Facades\DataTables;
class CargoSpecificationController extends Controller
{
public function index(Request $request){
if ($request->ajax()) {
$cargo_brand = CargoBrand::query()
->orderBy('created_at', 'desc');
return DataTables::of($cargo_brand)
->addColumn('status', function ($row) {
$checked = $row->status ? 'checked' : '';
return '
<label class="switch">
<input type="checkbox" class="toggle-status" data-id="'.$row->id.'" '.$checked.'>
<span class="slider round"></span>
</label>';
})
->addColumn('action', function ($row) {
$editUrl = route('cargo.edit', $row->id);
return '<a href="'.$editUrl.'" class="btn btn-sm btn-dark"><i class="fa fa-pencil-alt"></i></a>';
})
->rawColumns(['action','status'])
->make(true);
}
return view('admin.cargo_brand.index');
}
public function create(){
return view('admin.cargo_brand.create');
}
public function store(Request $request)
{
// Step 1: Validate input
$request->validate([
'name' => 'required|unique:cargo_brands,name',
], [
'name.required' => 'The cargo brand name is required.',
'name.unique' => 'This cargo brand name already exists.',
]);
// Step 2: Try to save
try {
$cargo_brand = new CargoBrand;
$cargo_brand->name = ucfirst(strtolower($request->name));
$cargo_brand->save();
return redirect()->route('cargo.brand')->with('success', 'Cargo brand created successfully.');
} catch (\Exception $e) {
return redirect()->route('cargo.brand')->with('error', 'Failed to create cargo brand. ' . $e->getMessage());
}
}
public function edit($id){
$cargo_edit = CargoBrand::find($id);
return view('admin.cargo_brand.edit',compact('cargo_edit'));
}
public function update(Request $request, $id)
{
// Step 1: Validate input
$request->validate([
'name' => 'required|unique:cargo_brands,name,' . $id,
], [
'name.required' => 'The cargo brand name is required.',
'name.unique' => 'This cargo brand name already exists.',
]);
// Step 2: Try to update
try {
$cargo_brand = CargoBrand::findOrFail($id);
$cargo_brand->name = ucfirst(strtolower($request->name));
$cargo_brand->save();
return redirect()->route('cargo.brand')->with('success', 'Cargo brand updated successfully.');
} catch (\Exception $e) {
return redirect()->route('cargo.brand')->with('error', 'Failed to update cargo brand. ' . $e->getMessage());
}
}
public function toggleCargoBrandStatus(Request $request){
$cargo_brand = CargoBrand::find($request->id);
if (!$cargo_brand) {
return response()->json([
'success' => false,
'message' => 'Cargo Brand not found.'
]);
}
$cargo_brand->status = $request->status;
$cargo_brand->update();
return response()->json([
'success' => true,
'message' => 'Status updated successfully.'
]);
}
// cargo Models------------------
public function model_index(Request $request){
if ($request->ajax()) {
$cargo_model = CargoModel::with('brand')->orderBy('created_at', 'desc');
return DataTables::of($cargo_model)
->addColumn('action', function ($row) {
$editUrl = route('cargo.model.edit', $row->id);
return '<a href="'.$editUrl.'" class="btn btn-sm btn-dark"><i class="fa fa-pencil-alt"></i></a>';
})
->addColumn('brand_name', function ($row) {
return $row->brand ? $row->brand->name : '-';
})
->addColumn('status', function ($row) {
$checked = $row->status ? 'checked' : '';
return '
<label class="switch">
<input type="checkbox" class="toggle-status" data-id="'.$row->id.'" '.$checked.'>
<span class="slider round"></span>
</label>';
})
->rawColumns(['action','status'])
->make(true);
}
return view('admin.cargo_model.index');
}
public function model_create(){
$cargo_brand = CargoBrand::where('status',CargoBrand::STATUS_ACTIVE)->get();
return view('admin.cargo_model.create',compact('cargo_brand'));
}
public function model_store(Request $request)
{
// Step 1: Validate input
$request->validate([
'name' => 'required|unique:cargo_models,name',
], [
'name.required' => 'The model name is required.',
'name.unique' => 'This model name already exists.',
]);
// Step 2: Try to save
try {
$cargo_brand = new CargoModel;
$cargo_brand->name = ucfirst(strtolower($request->name)); // Capitalize first letter
$cargo_brand->cargo_brand_id = $request->cargo_brand_id;
$cargo_brand->save();
return redirect()->route('cargo.model')->with('success', 'Model created successfully.');
} catch (\Exception $e) {
return redirect()->route('cargo.model')->with('error', 'Something went wrong. Please try again.');
}
}
public function model_edit($id){
$cargo_model = CargoModel::find($id);
$cargo_brand = CargoBrand::where('status',CargoBrand::STATUS_ACTIVE)->get();
return view('admin.cargo_model.edit',compact('cargo_model','cargo_brand'));
}
public function model_update(Request $request, $id)
{
// Step 1: Validate input
$request->validate([
'name' => 'required|unique:cargo_models,name,' . $id,
], [
'name.required' => 'The cargo model name is required.',
'name.unique' => 'This cargo model name already exists.',
]);
try {
$cargo_model = CargoModel::findOrFail($id);
$cargo_model->name = ucfirst(strtolower($request->name));
$cargo_model->save();
return redirect()->route('cargo.model')->with('success', 'Model updated successfully.');
} catch (\Exception $e) {
return redirect()->route('cargo.model')->with('error', 'Something went wrong while updating the model.');
}
}
public function toggleCargoModelStatus(Request $request){
$cargo_model = CargoModel::find($request->id);
if (!$cargo_model) {
return response()->json([
'success' => false,
'message' => 'Cargo Model not found.'
]);
}
$cargo_model->status = $request->status;
$cargo_model->update();
return response()->json([
'success' => true,
'message' => 'Status updated successfully.'
]);
}
// cargo Models------------------
public function type_index(Request $request){
if ($request->ajax()) {
$cargo_type = CargoType::query()
->orderBy('created_at', 'desc');
return DataTables::of($cargo_type)
->addColumn('action', function ($row) {
$editUrl = route('cargo.type.edit', $row->id);
return '<a href="'.$editUrl.'" class="btn btn-sm btn-dark"><i class="fa fa-pencil-alt"></i></a>';
})
->addColumn('status', function ($row) {
$checked = $row->status ? 'checked' : '';
return '
<label class="switch">
<input type="checkbox" class="toggle-status" data-id="'.$row->id.'" '.$checked.'>
<span class="slider round"></span>
</label>';
})
->rawColumns(['action','status'])
->make(true);
}
return view('admin.cargo_type.index');
}
public function type_create(){
return view('admin.cargo_type.create');
}
public function type_store(Request $request)
{
// Validate input
$request->validate([
'name' => 'required|unique:cargo_types,name',
], [
'name.required' => 'The type name is required.',
'name.unique' => 'This type name already exists.',
]);
try {
$cargo_type = new CargoType;
$cargo_type->name = ucfirst(strtolower($request->name));
$cargo_type->save();
return redirect()->route('cargo.type')->with('success', 'Cargo type created successfully.');
} catch (\Exception $e) {
return redirect()->route('cargo.type')->with('error', 'Something went wrong while creating the cargo type.');
}
}
public function type_edit($id){
$cargo_type = CargoType::find($id);
return view('admin.cargo_type.edit',compact('cargo_type'));
}
public function type_update(Request $request, $id)
{
// Validate input
$request->validate([
'name' => 'required|unique:cargo_types,name,' . $id,
], [
'name.required' => 'The type name is required.',
'name.unique' => 'This type name already exists.',
]);
try {
$cargo_type = CargoType::findOrFail($id);
$cargo_type->name = ucfirst(strtolower($request->name));
$cargo_type->save();
return redirect()->route('cargo.type')->with('success', 'Cargo type updated successfully.');
} catch (\Exception $e) {
return redirect()->route('cargo.type')->with('error', 'Something went wrong while updating the cargo type.');
}
}
public function toggleCargoTypeStatus(Request $request){
$cargo_type = CargoType::find($request->id);
if (!$cargo_type) {
return response()->json([
'success' => false,
'message' => 'Cargo Type not found.'
]);
}
$cargo_type->status = $request->status;
$cargo_type->update();
return response()->json([
'success' => true,
'message' => 'Status updated successfully.'
]);
}
}